﻿using BlogManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BlogManager.Service
{
    public class ComentarioRepository : IComentarioRepository
    {
        private static List<Comentario> Comentarios = new List<Comentario>();
        private static int _nextId = 1;

        public ComentarioRepository()
        {
            //Add(new Post { DateHora = DateTime.Now, Autor = "Paulo", Categoria = "Novo", Conteudo = "Nada", Idpost = 1 });
            //Add(new Post { DateHora = DateTime.Now, Autor = "Ingrid", Categoria = "Carro", Conteudo = "Palio", Idpost = 1 });
        }

        public List<Comentario> GetAll()
        {
            return Comentarios;
        }

        public Comentario Get(int id)
        {
            return Comentarios.Find(c => c.Id == id);
        }

        public Comentario Add(Comentario item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            item.Id = _nextId++;
            Comentarios.Add(item);
            return item;
        }

        public void Remove(int id)
        {
            Comentarios.RemoveAll(c => c.Id == id);
        }

        public void RemoveByIdpost(int idPost)
        {
            Comentarios.RemoveAll(c => c.IdPost == idPost);
        }

        public bool Update(Comentario item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            int index = Comentarios.FindIndex(c => c.Id == item.Id);
            if (index == -1)
            {
                return false;
            }
            Comentarios.RemoveAt(index);
            Comentarios.Add(item);
            return true;
        }

        public List<Comentario> GetByIdpost(int IdPost)
        {
            return new List<Comentario>(Comentarios.Where(c => c.IdPost == IdPost));
        }

    }
}